package com.qire.manhua.controller;

import android.view.View;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.google.android.material.appbar.AppBarLayout;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.databinding.FragmentClassificationBinding;
import com.qire.manhua.fragment.ClassificationFragment;
import com.qire.manhua.model.enumeration.Classification;
import com.qire.manhua.model.viewModel.impl.ClassificationViewModel;
import com.qire.manhua.model.viewModel.impl.ClassificationViewModel.ClassifiedTagItemViewModel;
import com.qire.manhua.model.viewModel.impl.ClassificationViewModel.ClassifiedComicItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class ClassificationController extends EventController<ClassificationFragment> implements AutoVariable {

    private ClassificationViewModel viewModel;

    public ClassificationController(@NonNull ClassificationFragment owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        FragmentClassificationBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setClassificationController(this);
        }
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.classificationController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ClassificationViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**********************************************************************************************/

    public void refreshLayoutOnLoadMore(RefreshLayout refreshLayout) {
        viewModel.loadMoreClassifiedComicList();
    }

    public void refreshLayoutOnRefresh(RefreshLayout refreshLayout) {
        viewModel.loadClassifiedComicList();
    }

    public void onSearchBarClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.ComicSearchPage).navigation();
        // 行为数据统计
        ComicActionRecorder.build(ComicActionRecorder.count_search_click)
                .setSource("分类页")
                .doRecorde(view.getContext());
    }

    public void appBarLayoutOnOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if(verticalOffset == 0) {
            //修改状态标记为展开
            if(viewModel.isFold()) {
                viewModel.toggleFold(Classification.FoldState.UNFOLD);
            }
        } else if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            //修改状态标记为折叠
            if(!viewModel.isFold()) {
                viewModel.toggleFold(Classification.FoldState.FOLD);
            }
        } else {
            //修改状态标记为中间
            if(viewModel.isFold()) {
                viewModel.toggleFold(Classification.FoldState.UNFOLD);
            }
        }
    }

    public void feeTypeBtnOnClick(View view) {
        Classification.FeeType feeType = (Classification.FeeType) view.getTag();
        viewModel.changeFeeType(feeType);
    }

    public void stateTypeBtnOnClick(View view) {
        Classification.StateType stateType = (Classification.StateType) view.getTag();
        viewModel.changeStateType(stateType);
    }

    public void sortTypeBtnOnClick(View view) {
        Classification.SortType sortType = (Classification.SortType) view.getTag();
        viewModel.changeSortType(sortType);
    }

    public void toggleSortTypeBtnOnClick(View view){
        Classification.SortType nextType = Classification.SortType.next(viewModel.sortType());
        viewModel.changeSortType(nextType);
    }

    public void closeCategoryBtnOnClick(View view){
        viewModel.defaultClassifiedTag().toggle();
    }

    public void closeStateTypeBtnOnClick(View view){
        viewModel.changeStateType(Classification.StateType.ALL);
    }

    public void closeFeeTypeBtnOnClick(View view) {
        viewModel.changeFeeType(Classification.FeeType.ALL);
    }

    public void foldBtnOnClick(View view) {
        if(viewModel.isFold()) {
            owner.viewDataBinder().classificationAppbarLayout.setExpanded(true);
        } else {
            owner.viewDataBinder().classificationAppbarLayout.setExpanded(false);
        }
    }

    public void classificationItemOnClick(View view){
        ClassifiedTagItemViewModel itemViewModel = (ClassifiedTagItemViewModel) view.getTag();
        itemViewModel.toggle();
    }

    public void classifiedComicItemOnClick(View view) {
        ClassifiedComicItemViewModel itemViewModel = (ClassifiedComicItemViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDetailPage)
                .withData("comicBookId", itemViewModel.comicId())
                .navigation();
    }

    public void reLoadDataOnClick(View view) {
        viewModel.loadClassifiedComicList();
    }


}
